//
//  UIViewController+SelectPhoto.m
//  WideHelp
//
//  Created by ENERGY on 2018/8/6.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "UIViewController+SelectPhoto.h"
#import <objc/runtime.h>

static NSString const *selectPhotoHandleKey = @"selectPhotoHandleKey";
static NSString const *shareUrlKey = @"shareUrlKey";


@implementation UIViewController (SelectPhoto)
@dynamic selectPhotoHandle;
@dynamic shareUrl;

- (void (^)(UIImage *))selectPhotoHandle
{
    return objc_getAssociatedObject(self, &selectPhotoHandleKey);
}

- (void)setSelectPhotoHandle:(void (^)(UIImage *))selectPhotoHandle
{
    objc_setAssociatedObject(self, &selectPhotoHandleKey,selectPhotoHandle,OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)shareUrl
{
    return objc_getAssociatedObject(self, &shareUrlKey);
}

- (void)setShareUrl:(NSString *)shareUrl
{
    objc_setAssociatedObject(self, &shareUrlKey,shareUrl,OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void)selectPhotoAlbumWithSelectPhotoHandle:(void (^)(UIImage *))selectPhotoHandle
{
    self.selectPhotoHandle = selectPhotoHandle;
    
    WS(ws)
    UIAlertController *aC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [ws takePhoto];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [ws LocalPhoto];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [aC addAction:action1];
    [aC addAction:action2];
    [aC addAction:cancelAction];
    [self presentViewController:aC animated:YES completion:nil];
}

- (void)takePhoto
{
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = NO;
        picker.sourceType = sourceType;
        [self presentViewController:picker animated:YES completion:nil];
    }
    else
    {
        NSLog(@"模拟其中无法打开照相机,请在真机中使用");
    }
}

- (void)LocalPhoto
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    picker.allowsEditing = NO;
    [self presentViewController:picker animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    if (self.selectPhotoHandle) {
        self.selectPhotoHandle(image);
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}

//获取当前屏幕显示的viewcontroller
+ (UIViewController *)getCurrentVC
{
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return  [UIViewController getPushVc:result];
}

+ (UIViewController *)getPushVc:(UIViewController *)result
{
    while ([result isKindOfClass:[UITabBarController class]]||[result isKindOfClass:[UINavigationController class]])
    {
        if ([result isKindOfClass:[UITabBarController class]])
        {
            UITabBarController *tabbar = (UITabBarController *)result;
            UIViewController *vc = tabbar.selectedViewController;
            result = vc;
        }
        
        if ([result isKindOfClass:[UINavigationController class]])
        {
            UINavigationController *nav = (UINavigationController *)result;
            UIViewController *vc = nav.topViewController;
            result = vc;
        }
    }
    
    return [UIViewController getPresent:result];
}

+ (UIViewController *)getPresent:(UIViewController *)result
{
    if (result.presentedViewController == nil)
    {
        return result;
    }
    return [UIViewController getPushVc:result.presentedViewController];
}

@end
